package com.reyco1.medusa.loading
{
	import com.reyco1.medusa.debug.Debugger;
	
	import flash.net.URLRequest;

	public class LoadingItem
	{
		private const VISUAL:String 	 = "visual";
		private const DATA:String 		 = "data";
		private const TEXT_TYPES:Array 	 = ["xml", "txt", "php", "asp", "css", "html", "rss"];
		private const VISUAL_TYPES:Array = ["gif", "jpeg", "jpg", "png", "swf"];
		
		private var _loader:ILoader;		
		private var _url:String;
		private var _content:*;
		private var _id:String;
		private var _loaded:Boolean;
		private var _name:String;
		private var _index:int;		
		private var _noCache:Boolean;
		
		public function LoadingItem(path:String, noCache:Boolean = false)
		{
			this.noCache = noCache;
			url = path;
		}
		
		/* private */
		
		public function configure(file:String):void
		{
			var parts:Array = file.split(".");
			
			if(parts.length < 2)
			{
				Debugger.log("The file you are trying to load has no extension. This is the file path you provided - " + file, this, "fatal");
			}
			
			_id = _name = extractFileNameFromPath(file);
			
			var ext:String = parts[parts.length-1].toLowerCase();			
			var fileType:String = DATA;
			var a:Number;
			var params:String = noCache ? "?noCache="+Math.random()*1000 : "";
			
			_loader = new DataLoader(new URLRequest(_url+params)) as ILoader;
			
			for(a = 0; a<VISUAL_TYPES.length; a++)
			{
				if(ext == VISUAL_TYPES[a])
				{
					_loader = new DisplayLoader(new URLRequest(_url+params)) as ILoader;
					break;
				}
			}
		}
		
		private function extractFileNameFromPath(path:String):String
		{
			var pathElements:Array = path.split("/");
			var dirtyFileName:Array = pathElements[pathElements.length - 1].split(".");
			var fileName:String = dirtyFileName[0];
			return fileName;
		}
		
		/* properties */

		public function get url():String
		{
			return _url;
		}

		public function set url(value:String):void
		{
			_url = value;
			configure(value);
		}

		public function get content():*
		{
			return _content;
		}

		public function set content(value:*):void
		{
			_content = value;
		}

		public function get id():String
		{
			return _id;
		}

		public function set id(value:String):void
		{
			_id = value;
		}

		public function get loaded():Boolean
		{
			return _loaded;
		}

		public function set loaded(value:Boolean):void
		{
			_loaded = value;
		}

		public function get name():String
		{
			return _name;
		}

		public function set name(value:String):void
		{
			_name = value;
		}

		public function get loader():ILoader
		{
			return _loader;
		}

		public function set loader(value:ILoader):void
		{
			_loader = value;
		}

		public function get index():int
		{
			return _index;
		}

		public function set index(value:int):void
		{
			_index = value;
		}

		public function get noCache():Boolean
		{
			return _noCache;
		}

		public function set noCache(value:Boolean):void
		{
			_noCache = value;
		}


	}
}